function templateCenterDiv(divname) {
    var cH = $("#" + divname).height();
    var cW = $("#" + divname).width();
    var wH = $(window).height();
    var wW = $(window).width();
    if ((wH - cH) > 0) {
        $("#" + divname).css('top', ((wH - cH) / 2) + "px");
    }
    if ((wW - cW) > 0) {
        $("#" + divname).css('left', ((wW - cW) / 2) + "px");
    }
}

function mainPageTemplateSet(divname) {
    var cW = $("#" + divname).width();
    var wH = $(window).height() - 20;
    var wW = $(window).width();
    if (wH <= 600) {
        $("#" + divname).css('height', "600px");
    } else {
        $("#" + divname).css('height', wH + "px");
    }
    var cH = $("#" + divname).height();
    var menuH = $("#menu").height();
    var footerH = $("#footer").height();
    $("#container2").css('height', (cH - menuH - footerH) + "px");
    $("#content").css('max-height', (cH - menuH - footerH) + "px");
    $("#leftMenu").css('max-height', (cH - menuH - footerH) + "px");
}

$(document).ready(function() {
    // KOD JS DLA STRONY LOGOWANIA
    if (pageType === "loginPage") {
        $(document).tooltip();
        $(function() {
            templateCenterDiv("loginContainer");
            templateCenterDiv("loginProgressDiv");

            /// FORMULARZ LOGOWANIA
            $.ajax({
                url: 'ajaxhelper.php?action=getAjaxLoader',
                success: function(html) {
                    $("#ajaxLoader").html(html);
                    $("#ajaxLoader2").html(html);
                }
            });

            $("#validateEmailCell").hide();
            $("#ajaxLoader").hide();
            $("#btnLoginPage").button({disabled: true});
            $("#btnRegPage").button();
            $("#loginContainer").hide();
            $("#tabs").tabs();
            $("#loginProgressDiv").hide();
            $("#loginContainer").delay(500).fadeIn("slow");
            $("#inpLoginPageEmail").keyup(function() {
                $("#ajaxLoader").show();
                var inpCont = $("#inpLoginPageEmail").val();
                var aHelpGetVar = {action: 'userExists', email: inpCont};
                $.get('ajaxhelper.php', aHelpGetVar).done(function(html) {
                    if (html === "1") {
                        $("#ajaxLoader").hide();
                        $("#btnLoginPage").button({disabled: false}).click(function() {
                            $("#inpLoginPageEmail").attr('disabled', 'disabled');
                            $("#btnLoginPage").attr('disabled', 'disabled');
                            $("#loginContainer").fadeOut("slow", function() {
                                $("#loginProgressDiv").fadeIn("slow");
                                var inpCont = $("#inpLoginPageEmail").val();
                                $.get('ajaxhelper.php', {action: 'sendLoginRequest', email: inpCont})
                                        .done(function(html) {
                                    if (html === "1") {
                                        $("#loginProgressDiv").html($("#msgLoginPass").html());
                                        setTimeout(function() {
                                            window.close();
                                        }, 2000);
                                    } else {
                                        $("#loginProgressDiv").html($("#msgLoginFail").html());
                                        setTimeout(function() {
                                            window.close();
                                        }, 2000);
                                    }
                                    templateCenterDiv("loginProgressDiv");
                                });
                            });
                        });
                    } else {
                        $("#ajaxLoader").hide();
                        $("#btnLoginPage").button({disabled: true});
                    }
                });
            });

            /// FORMULARZ REJESTRACI

            $("#regPageRegl").button();
            $("#iconRegMail").hide();
            $("#iconRegState").hide();
            $("#iconRegCity").hide();
            $("#iconRegNick").hide();
            $("#iconRegReg").hide();
            //$("#dialogRegPage").hide();

            var vmail, vstate, vcity, vnick, vreg = false;

            $("#regPageRegl").click(function() {
                $("#iconRegReg").show();
                $("#regPageRegl").hide();
                vreg = true;
            });

            $("#regPageNick").keyup(function() {
                var nickToVal = $("#regPageNick").val();
                var getParams = {
                    action: 'validateNick',
                    nick: nickToVal
                };
                $.get("ajaxhelper.php", getParams).done(function(html) {
                    if (html === "1") {
                        $("#iconRegNick").show();
                        vnick = true;
                    } else {
                        $("#iconRegNick").hide();
                        vnick = false;
                    }
                });
            });

            $("#inpRegMail").keyup(function() {
                var emailToVal = $("#inpRegMail").val();
                var getParams = {
                    action: 'validateEmail',
                    email: emailToVal
                };
                $.get("ajaxhelper.php", getParams).done(function(html) {
                    if (html === "1") {
                        $("#validateEmailCell").show();
                    } else {
                        $("#validateEmailCell").hide();
                    }
                });
            });

            $("#ancValidateEmail").click(function() {
                $("#validateEmailCell").hide();
                $("#inpRegMail").attr('disabled', 'disabled');
                vmail = true;
            });

            $.get("ajaxhelper.php", {action: 'getStatesForm', name: 'inpRegState'}).done(function(html) {
                $("#inpRegStateDiv").html(html);
                var regState = $("#inpRegState option:selected").val();
                $("#inpRegState").change(function() {
                    regState = $("#inpRegState option:selected").val();
                    $.get("ajaxhelper.php", {action: 'getCitiesFromIdForm', id: regState, name: 'inpRegCity'}).done(function(html) {
                        $("#inpRegCityDiv").html(html);
                    });
                    var regCity = $("#inpRegCity option:selected").val();
                    $("#inpRegState").change(function() {
                        regCity = $("#inpRegCity option:selected").val();
                    });
                    if (regState !== "99") {
                        $("#iconRegState").fadeIn("slow");
                        $("#iconRegCity").fadeIn("slow");
                        vstate = true;
                        vcity = true;
                    } else {
                        $("#iconRegState").fadeOut("slow");
                        $("#iconRegCity").fadeOut("slow");
                        vstate = false;
                        vcity = false;
                    }
                });
            });

            function checkreg() {
                if (vmail && vstate && vcity && vnick && vreg) {
                    return true;
                } else {
                    return false;
                }
            }

            $("#btnRegPage").click(function() {
                if (checkreg()) {
                    $("#loginContainer").fadeOut("slow", function() {
                        $("#loginProgressDiv").fadeIn("slow", function() {
                            var m, n, c, s;
                            m = $("#inpRegMail").val();
                            n = $("#regPageNick").val();
                            c = $("#inpRegCity option:selected").val();
                            s = $("#inpRegState option:selected").val();
                            var getRegVals = {
                                action: 'sendRegistrationForm',
                                email: m,
                                nick: n,
                                city: c,
                                state: s
                            };
                            $.get('ajaxhelper.php', getRegVals).done(function(html) {
                                if (html === "1") {
                                    $("#loginProgressDiv").html($("#msgLoginPass").html());
                                    setTimeout(function() {
                                        window.close();
                                    }, 2000);
                                } else {
                                    $("#loginProgressDiv").html($("#msgLoginFail").html());
                                    setTimeout(function() {
                                        window.close();
                                    }, 2000);
                                }
                                templateCenterDiv("loginProgressDiv");
                            });
                        });
                    });
                } else {
                    alert("Sprawdź czy wszystkie pola zostały wypełnione!");
                }
            });

        });
    } else if (pageType === "mainPage") {
        //profilki
        var aplW = $("#profileApletBigAncx").width();
        var aplH = $("#profileApletBigAnc").height();

        $("#profileApletBigAnc").css('top', -aplH - 5 + "px");
        $("#profileApletBigAnc").css('left', (64 - aplW) + "px");
        $("#dlgNewMsg").dialog({
            modal: true,
            close: function() {
                $("#msgNew").effect("pulsate", {times: 5}, 2000);
            }
        });
        $("#dlgOSucc").dialog({
            modal: true,
        });

        mainPageTemplateSet("container");
    }
});

$(window).resize(function() {
    if (pageType === "loginPage") {
        templateCenterDiv("loginContainer");
        templateCenterDiv("loginProgressDiv");
    } else if (pageType === "mainPage") {
        mainPageTemplateSet("container");
    }
});

var searchBarClicked = false;
function clickSearchBarInput() {
    if (!searchBarClicked) {
        searchBarClicked = true;
        var sbInput = document.getElementById('searchBarInput');
        sbInput.value = '';
    }
}